package com.gdut.simple.presenter;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.FollowContract;
import com.gdut.simple.contract.FollowUserContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IFollowModel;
import com.gdut.simple.model.IFollowUserModel;
import com.gdut.simple.model.impl.FollowModelImpl;
import com.gdut.simple.model.impl.FollowUserModelImpl;

import java.util.List;

public class FollowUserPresenter implements FollowUserContract.Presenter {

    private FollowUserContract.View mFollowUserView;
    private IFollowUserModel mFollowUserModel;

    public FollowUserPresenter(FollowUserContract.View mFollowView) {
        this.mFollowUserView = mFollowView;
        mFollowUserModel = new FollowUserModelImpl();
        this.mFollowUserView.setPresenter(this);
    }

    @Override
    public void refresh() {
        if (mFollowUserView.getUser() == null) {
            return;
        }
        mFollowUserView.showRefreshProgress();
        mFollowUserModel.getFollowList(mFollowUserView.getUser(), 1, new RequestCallback<List<User>>() {
            @Override
            public void onSuccess(List<User> users) {
                mFollowUserView.hideRefreshProgress();
                mFollowUserView.getItemList().clear();
                mFollowUserView.getItemList().addAll(users);
                mFollowUserView.refreshList();
                mFollowUserView.reset();
            }

            @Override
            public void onFailure(String message) {
                mFollowUserView.hideRefreshProgress();
                mFollowUserView.showMessage(message);
            }
        });
    }

    @Override
    public void loadMore(int page) {
        mFollowUserModel.getFollowList(mFollowUserView.getUser(), page, new RequestCallback<List<User>>() {
            @Override
            public void onSuccess(List<User> users) {
                mFollowUserView.getItemList().clear();
                mFollowUserView.getItemList().addAll(users);
                mFollowUserView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mFollowUserView.showMessage(message);
            }
        });
    }
}
